package com.chanyachting.emall.action.channel;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.form.ChannelForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class ChannelAction extends DispatchAction {


	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        
        ChannelService cs=new ChannelServiceImpl();
        
        //如果是创建频道请求
        if(request.getMethod().equals("POST")){
        	
        	ChannelForm channelForm=(ChannelForm)form;
        	
        	//如果验证成功
        	if(channelForm.validate(mapping, request).isEmpty()){
        		
        		Channel channel=new Channel();
        		
        		BeanUtils.copyProperties(channel, channelForm);
        		
        		channel.setCreateDate(DateUtil.getDate());
        		
        		channel.setStatus("01");  //创建频道，默认公开
        		
        		cs.createChannel(channel);
        		
        		new RequestUtil(request).setSuccessMsg();
        		
        	}
        }
		
        
        //获得所有父频道列表
        List parentlist=cs.getAllParentChannel();
        
        new RequestUtil(request).setAttribute("parenChanneltlist", parentlist);
		
		return mapping.findForward("create");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ChannelService cs=new ChannelServiceImpl();
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			ChannelForm channelForm=(ChannelForm)form;
        	
        	//如果验证成功
        	if(channelForm.validate(mapping, request).isEmpty()){
        		
        		Channel channel=new Channel();
        		
        		BeanUtils.copyProperties(channel, channelForm);
        		
        		cs.updateChannel(channel);  //执行更新
        		
        		new RequestUtil(request).setSuccessMsg();
        		
        	}
			
		}
		
		//重新加载频道对象
		Channel channel=new Channel();
		
		channel.setId(id);
		
		channel=cs.getOneChannel(channel);
		
		ru.setAttribute("channel", channel);
		
		//加载父频道列表
		List parentlist=cs.getAllParentChannel();
        
        ru.setAttribute("parenChanneltlist", parentlist);

		return mapping.findForward("update");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ChannelService cs=new ChannelServiceImpl();
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Channel channel=new Channel();
		
		channel.setId(id);
		
		cs.deleteChannel(channel);
		
		return mapping.findForward("delete");
	}
	
	
	
	public ActionForward parentList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("channelList", Channel.parentList());
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward subList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int pid=Integer.parseInt(ru.getParameter("pid", "0"));
		
		Channel pchannel=new Channel(pid);
		
		ru.setAttribute("channelList", pchannel.getSubList());
		
		ru.setAttribute("pchannel", pchannel);
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		  List list=new ArrayList();
		  
		  ChannelService cs=new ChannelServiceImpl();
		  
		  list=cs.getAllChannel();
		  
		  new RequestUtil(request).setAttribute("channelList", list);
		
		
		return mapping.findForward("list");
	}
	
	
	

}
